<template>
  <view v-if="show" class="tui-modal__container" :class="[show ? 'tui-modal-show' : '']" :style="{ zIndex: zIndex, alignItems: top ? '' : 'center', height: top ? '' : '100%' }">
    <view
      class="tui-modal-box"
      :style="{ top: top, width: width, padding: padding, borderRadius: radius, backgroundColor: backgroundColor, zIndex: zIndex + 1 }"
      :class="[fadeIn || show ? 'tui-modal-normal' : 'tui-modal-scale', show ? 'tui-modal-show' : '']"
    >
      <view>
        <header class="header">
          <view class="header-title" v-if="title">{{ title }}</view>
          <tui-icon v-if="showClose" name="close" size="22" color="white" class="header-close-icon" @click="close"></tui-icon>
        </header>

        <slot></slot>

        <view v-if="button && button.length > 0" class="tui-modalBtn-box" :class="[button.length != 2 ? 'tui-flex-column' : '']">
          <block v-for="(item, index) in button" :key="index">
            <button
              class="tui-modal-btn"
              :class="[
                'tui-' + (item.type || 'primary') + (item.plain ? '-outline' : ''),
                button.length != 2 ? 'tui-btn-width' : '',
                button.length > 2 ? 'tui-mbtm' : '',
                shape == 'circle' ? 'tui-circle-btn' : ''
              ]"
              :hover-class="'tui-' + (item.plain ? 'outline' : item.type || 'primary') + '-hover'"
              :data-index="index"
              :open-type="item.openType || ''"
              @tap="clickBtn(item, index)"
            >
              {{ item.text || '确定' }}
            </button>
          </block>
        </view>
      </view>
    </view>
    <view class="tui-modal-mask" :class="[show ? 'tui-mask-show' : '']" :style="{ zIndex: maskZIndex, background: maskBg }" @tap="clickModal" @touchmove.stop.prevent></view>
  </view>
</template>

<script>
export default {
  name: 'tuiModal',
  props: {
    top: {
      type: String,
      default: ''
    },
    width: {
      type: String,
      default: '84%'
    },
    backgroundColor: {
      type: String,
      default: '#313954'
    },
    padding: {
      type: String,
      default: '0'
    },
    radius: {
      type: String,
      default: '5px'
    },
    // 标题
    title: {
      type: String,
      default: ''
    },
    // 内容
    content: {
      type: String,
      default: ''
    },
    // 内容字体颜色
    color: {
      type: String,
      default: '#999'
    },
    // 内容字体大小 rpx
    size: {
      type: Number,
      default: 28
    },
    // 形状 circle, square
    shape: {
      type: String,
      default: 'square'
    },
    button: {
      type: Array,
      default: null
      // function() {
      //   return [
      //     {
      //       text: '取消',
      //       type: 'red',
      //       plain: true //是否空心
      //     },
      //     {
      //       text: '确定',
      //       type: 'red',
      //       plain: false
      //     }
      //   ]
      // }
    },
    // 点击遮罩 是否可关闭
    maskClosable: {
      type: Boolean,
      default: true
    },
    // 淡入效果，自定义弹框插入input输入框时传true
    fadeIn: {
      type: Boolean,
      default: true
    },
    // 自定义弹窗内容
    custom: {
      type: Boolean,
      default: false
    },
    // 容器 z-index
    zIndex: {
      type: Number,
      default: 9997
    },
    // mask z-index
    maskZIndex: {
      type: Number,
      default: 9990
    },
    maskBg: {
      type: String,
      default: 'rgba(0, 0, 0, 0.6)'
    }
  },
  data() {
    return {
      // 是否显示模态框
      show: false,
      // 是否显示头部关闭按钮
      showClose: true
    }
  },
  methods: {
    open(param) {
      this.showClose = this.$format(param, 'showClose', true)
      this.show = true
    },
    close() {
      this.show = false
      this.$emit('afterModalClose')
    },
    clickBtn(item, index) {
      if (!this.show) return

      console.log('@clickBtn 可以监听该事件')
      this.$emit('clickBtn', { index: Number(index), data: item })
    },
    clickModal() {
      console.log('@clickModal 可以监听该事件')

      if (!this.maskClosable) return

      // 如果允许用户关闭模态框的话，点击遮罩层直接关闭模态框。 如果不允许的话向上传递事件由父组件处理
      if (this.showClose) {
        this.show = false
      }

      this.$emit('clickModal')
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  position: relative;
  padding: 12px;
  @include border-bottom;
  .header-title {
    text-align: center;
  }

  .header-close-icon {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: 10px;
  }
}

.tui-modal__container {
  width: 100%;
  position: fixed;
  left: 0;
  top: 0;
  display: flex;
  justify-content: center;
  visibility: hidden;
  transition: all 0.3s ease-in-out;
}
.tui-modal-box {
  position: relative;
  opacity: 0;
  visibility: hidden;
  box-sizing: border-box;
  transition: all 0.3s ease-in-out;
}

.tui-modal-scale {
  transform: scale(0);
}

.tui-modal-normal {
  transform: scale(1);
}

.tui-modal-show {
  opacity: 1;
  visibility: visible;
}

.tui-modal-mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  // background-color: rgba(0, 0, 0, 0.6);
  background-color: rgba(19, 24, 40, 0.7);
  transition: all 0.3s ease-in-out;
  opacity: 0;
  visibility: hidden;
}

.tui-mask-show {
  visibility: visible;
  opacity: 1;
}

.tui-modal-title {
  text-align: center;
  font-size: $font-size-base;
  color: #fefefe;
  font-weight: bold;
}

.tui-modal-content {
  text-align: center;
  color: #999;
  font-size: 28rpx;
  padding-top: 20rpx;
  padding-bottom: 60rpx;
}

.tui-mtop {
  margin-top: 30rpx;
}

.tui-mbtm {
  margin-bottom: 30rpx;
}

.tui-modalBtn-box {
  width: calc(100% - 30px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 15px auto;
}

.tui-flex-column {
  flex-direction: column;
}

.tui-modal-btn {
  width: 46%;
  height: 80rpx;
  line-height: 80rpx;
  position: relative;
  border-radius: 10rpx;
  font-size: 30rpx;
  overflow: visible;
  margin-left: 0;
  margin-right: 0;
}

.tui-modal-btn::after {
  content: ' ';
  position: absolute;
  width: 200%;
  height: 200%;
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
  transform: scale(0.5, 0.5) translateZ(0);
  left: 0;
  top: 0;
  border-radius: 20rpx;
  z-index: 2;
}

.tui-btn-width {
  width: 100% !important;
}

.tui-primary {
  background: #5677fc;
  color: #fff;
}

.tui-primary-hover {
  background: #4a67d6;
  color: #e5e5e5;
}

.tui-primary-outline {
  color: #5677fc;
  background: transparent;
}

.tui-primary-outline::after {
  border: 1px solid #5677fc;
}

.tui-danger {
  background: #ed3f14;
  color: #fff;
}

.tui-danger-hover {
  background: #d53912;
  color: #e5e5e5;
}

.tui-danger-outline {
  color: #ed3f14;
  background: transparent;
}

.tui-danger-outline::after {
  border: 1px solid #ed3f14;
}

.tui-red {
  background: #e41f19;
  color: #fff;
}

.tui-red-hover {
  background: #c51a15;
  color: #e5e5e5;
}

.tui-red-outline {
  color: #e41f19;
  background: transparent;
}

.tui-red-outline::after {
  border: 1px solid #e41f19;
}

.tui-warning {
  background: #ff7900;
  color: #fff;
}

.tui-warning-hover {
  background: #e56d00;
  color: #e5e5e5;
}

.tui-warning-outline {
  color: #ff7900;
  background: transparent;
}

.tui-warning-outline::after {
  border: 1px solid #ff7900;
}

.tui-green {
  background: #19be6b;
  color: #fff;
}

.tui-green-hover {
  background: #16ab60;
  color: #e5e5e5;
}

.tui-green-outline {
  color: #19be6b;
  background: transparent;
}

.tui-green-outline::after {
  border: 1px solid #19be6b;
}

.tui-white {
  background: #fff;
  color: #333;
}

.tui-white-hover {
  background: #f7f7f9;
  color: #666;
}

.tui-white-outline {
  color: #333;
  background: transparent;
}

.tui-white-outline::after {
  border: 1px solid #333;
}

.tui-gray {
  background: #ededed;
  color: #999;
}

.tui-gray-hover {
  background: #d5d5d5;
  color: #898989;
}

.tui-gray-outline {
  color: #999;
  background: transparent;
}

.tui-gray-outline::after {
  border: 1px solid #999;
}

.tui-outline-hover {
  opacity: 0.6;
}

.tui-circle-btn {
  border-radius: 40rpx !important;
}

.tui-circle-btn::after {
  border-radius: 80rpx !important;
}

.tui-gold {
  background: $color-gold-bg;
  color: $color-primary-bg;
}

.tui-gold-hover {
  background: linear-gradient(180deg, #ffeac4, #f0c08b);
  color: $color-primary-bg;
}

.tui-gold-outline {
  color: $color-primary-bg;
  background: transparent;
}
</style>
